package com.lebang.service.sys;

import com.alibaba.fastjson.JSON;
import com.lebang.component.RedisClient;
import com.lebang.domain.*;
import com.lebang.entity.RequestHeaders;
import com.lebang.entity.TaskRewards;
import com.lebang.entity.sys.AppMain;
import com.lebang.example.AppLookCategoryExample;
import com.lebang.mapper.AppLookCategoryMapper;
import com.lebang.mapper.AppStoreCheckMapper;
import com.lebang.service.BaseService;
import com.lebang.service.buss.MemberService;
import com.lebangweb.common.TextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;

/**
 * app_look_category
 *
 * @Author hwq
 * @Date 2020-06-23
 */
@Service
public class AppLookCategoryService extends BaseService<AppLookCategory, Long, AppLookCategoryExample> {
    @Autowired
    private AppLookCategoryMapper appLookCategoryMapper;
    @Autowired
    private RedisClient redisClient;
    @Autowired
    private MemberService memberService;

    public List<AppLookCategory> getAll() {
        return appLookCategoryMapper.getAll();
    }

    public void init() {
        redisClient.delOne("AppLookCategory");
        selectAllList();

    }

    public List<AppLookCategory> selectAllList() {
        Optional<String> str = Optional.ofNullable(redisClient.get("AppLookCategory"));
        if (str.isPresent()) {
            return JSON.parseArray(str.get(), AppLookCategory.class);
        } else {
            List<AppLookCategory> lookCategories = this.selectByExample(new AppLookCategoryExample());
            redisClient.set("AppLookCategory", JSON.toJSONString(lookCategories));
            return lookCategories;
        }
    }

    public List<AppResoure> reviewResoure(List<AppResoure> list, RequestHeaders headers, int money) {
        money=checkMember(money,headers);
        List<AppLookCategory> selectAllList = this.selectAllList();
        Iterator<AppResoure> iterator = list.iterator();
        while ( iterator.hasNext()) {
            AppResoure next = iterator.next();
            if (!TextUtil.isEmpty(next.getLookCategory())) {
                for (AppLookCategory category : selectAllList) {
                    if (category.check(headers.getAppStore(), next.getLookCategory())) {

                        if(!TextUtil.isEmpty(category.getVersion())&&headers.getVersionInt()>=category.getVersion()){
                            //不显示
                            if (money < category.getLookMoney()) {
                                iterator.remove(); break;
                            }
                        }
                    }
                }
            }
        }
        return list;
    }

    public List<RouseDayConfig> reviewRouse(List<RouseDayConfig> list, RequestHeaders headers, int money) {
        money=checkMember(money,headers);
        List<AppLookCategory> selectAllList = this.selectAllList();
        Iterator<RouseDayConfig> iterator = list.iterator();
        while ( iterator.hasNext()) {
            RouseDayConfig next = iterator.next();
            if (!TextUtil.isEmpty(next.getLookCategory())) {
                for (AppLookCategory category : selectAllList) {
                    if (category.check(headers.getAppStore(), next.getLookCategory())) {

                        if(!TextUtil.isEmpty(category.getVersion())&&headers.getVersionInt()>=category.getVersion()){
                            if (money < category.getLookMoney()) {
                                iterator.remove(); break;
                            }
                        }
                    }
                }
            }
        }
        return list;
    }

    public List<TaskRewards> reviewRewards(List<TaskRewards> list, RequestHeaders headers, int money) {
        money=checkMember(money,headers);
        List<AppLookCategory> selectAllList = this.selectAllList();
        Iterator<TaskRewards> iterator = list.iterator();
        while ( iterator.hasNext()) {
            TaskRewards next = iterator.next();
            if (!TextUtil.isEmpty(next.getLookCategory())) {
                for (AppLookCategory category : selectAllList) {
                    if (category.check(headers.getAppStore(), next.getLookCategory())) {

                        if(!TextUtil.isEmpty(category.getVersion())&&headers.getVersionInt()>=category.getVersion()){
                            if (money < category.getLookMoney()) {
                                iterator.remove(); break;
                            }
                        }
                    }
                }
            }
        }
        return list;
    }
    public List<CustomerMemberIssue> reviewIssue(List<CustomerMemberIssue> list, RequestHeaders headers, int money) {
        money=checkMember(money,headers);
        List<AppLookCategory> selectAllList = this.selectAllList();
        Iterator<CustomerMemberIssue> iterator = list.iterator();
        while ( iterator.hasNext()) {
            CustomerMemberIssue next = iterator.next();
            if (!TextUtil.isEmpty(next.getLookCategory())) {
                for (AppLookCategory category : selectAllList) {
                    if (category.check(headers.getAppStore(), next.getLookCategory())) {

                        if(!TextUtil.isEmpty(category.getVersion())&&headers.getVersionInt()>=category.getVersion()){
                            if (money < category.getLookMoney()) {
                                iterator.remove(); break;
                            }
                        }
                    }
                }
            }
        }
        return list;
    }

    public List<AppMain> reviewAppMain(List<AppMain> list, RequestHeaders headers, int money) {
        money=checkMember(money,headers);
        List<AppLookCategory> selectAllList = this.selectAllList();
        Iterator<AppMain> iterator = list.iterator();
        while ( iterator.hasNext()) {
            AppMain next = iterator.next();
            if (!TextUtil.isEmpty(next.getLookCategory())) {
                for (AppLookCategory category : selectAllList) {
                    if (category.check(headers.getAppStore(), next.getLookCategory())) {

                        if(!TextUtil.isEmpty(category.getVersion())&&headers.getVersionInt()>=category.getVersion()){
                            if (money < category.getLookMoney()) {
                                iterator.remove();
                                break;
                            }
                        }
                    }
                }
            }
        }
        return list;
    }

    @Autowired
    private AppStoreCheckMapper appStoreCheckMapper;

    public int checkMember(int money, RequestHeaders headers) {
        AppStoreCheck vo = appStoreCheckMapper.getByAppStore(headers.getAppStore());
        if(vo==null){
            return money;
        }
        if (vo.getOpenCheck() == 1 && vo.getIsCheck() == 1) {
            if (memberService.checkImeiAndIpAddr(headers)) {
                return 0;
            }
        }
        return money;
    }

    /**
     * 统计配置个数
     * @param vo
     * @return
     */
    public Integer countByVo(AppLookCategory vo) {
        return appLookCategoryMapper.countByVo(vo.getId(),vo.getAppStore(),vo.getCategory());
    }
}
